#include <sys_head.h>

int main(int argc, char const *argv[])
{
    //通过命令行参数 获取文件名
    if (2 != argc)
    {
        printf("参数错误:%s pathname\n" , argv[0]);
        return -1;
    }

    struct stat statbuf;
    stat(argv[1],&statbuf);

    //文件类型
    char permissions[11] = "----------";

    if (S_ISREG(statbuf.st_mode))
    {
        permissions[0] = '-';
    }else if (S_ISDIR(statbuf.st_mode))
    {
        permissions[0] = 'd';
    }else if (S_ISCHR(statbuf.st_mode))
    {
        permissions[0] = 'c';
    }else if (S_ISBLK(statbuf.st_mode))
    {
        permissions[0] = 'b';
    }else if (S_ISFIFO(statbuf.st_mode))
    {
        permissions[0] = 'p';
    }else if (S_ISLNK(statbuf.st_mode))
    {
        permissions[0] = 'l';
    }else if (S_ISSOCK(statbuf.st_mode))
    {
        permissions[0] = 's';
    }
    
    //文件权限
    if (statbuf.st_mode & S_IRUSR)
    {
        permissions[1] = 'r';
    }
    if (statbuf.st_mode & S_IWUSR)
    {
        permissions[2] = 'w';
    }
    if (statbuf.st_mode & S_IXUSR)
    {
        permissions[3] = 'x';
    }
    if (statbuf.st_mode & S_IRGRP)
    {
        permissions[4] = 'r';
    }
    if (statbuf.st_mode & S_IWGRP)
    {
        permissions[5] = 'w';
    }
    if (statbuf.st_mode & S_IXGRP)
    {
        permissions[6] = 'x';
    }
    if (statbuf.st_mode & S_IROTH)
    {
        permissions[7] = 'r';
    }
    if (statbuf.st_mode & S_IWOTH)
    {
        permissions[8] = 'w';
    }
    if (statbuf.st_mode & S_IXOTH)
    {
        permissions[9] = 'x';
    }
    
    //所属者姓名
    struct passwd * user;
    user = getpwuid(statbuf.st_uid);

    //用户组名称
    struct group * Group;
    Group = getgrgid(statbuf.st_gid);

    //时间
    struct tm * time_stu = localtime(&statbuf.st_mtime);

    printf("%s %ld %s %s %ld %d月 %d %d:%d %s\n",
            permissions , statbuf.st_nlink , 
            user->pw_name , Group->gr_name , statbuf.st_size , 
            time_stu->tm_mon + 1 , time_stu->tm_mday ,time_stu->tm_hour , time_stu->tm_min , argv[1]);
    return 0;
}
